<?php
/**
 * 会员中心模块微站定义
 *
 * @author 橙橙同学
 * @url 
 */
defined('IN_IA') or exit('Access Denied');
require_once 'bm/bootstrap.php';

use \app\Model\MemberModel;
class Orange_memberModuleSite extends WeModuleSite {

    public function doMobileDevBusinessMember()
    {
        include $this->template('member/businessmember');
    }

    public function doMobileDevCompanyMember()
    {
        include $this->template('member/companymember');
    }


    /**
     * @param $message
     * @author:橙橙同学
     * @func:公共提示
     */
    protected function commonTips($message,$redirect = '',$type)
    {
        if($redirect == '')
        {
            $redirectUrl = $_SERVER['HTTP_REFERER'];
        }else{
            $redirectUrl = $redirect;
        }
        switch ($type){
            case "success":
                message($message,$redirectUrl,'success');
//                $this->template('commontips/success');
                break;
            case "info":
                message($message,$redirectUrl,'info');
//                $this->template('commontips/success');
                break;
            case "error":
                message($message,$redirectUrl,'error');
//                $this->template('commontips/error');
                break;
            default:
                message($message,$redirectUrl,'success');
//                $this->template('commontips/success');
        }

    }

    protected function commonApiCheckOpenid($openid)
    {
        if(empty($openid))
        {
            $this->commonApiReturn(-1,'','请在微信端打开本页面');
            exit();
        }
    }

    protected function commonApicheckUid($uid)
    {
        if(empty($uid))
        {
            $this->commonApiReturn(-1,'','您还不是会员');
            exit();
        }
    }


    /**
     * @author:橙橙同学
     * @func:公共检测微信
     */
    protected function commonCheckOpenid()
    {
        global $_W;
        $openid = $_W['openid'];
        if(empty($openid))
        {
            $this->commonTips('请确保在微信端打开本页面','','info');
            exit();
        }
    }


    /**
     * @param $openid
     * @author:橙橙同学
     * @func:公共检测会员
     */
    protected function commonCheckUid($uid)
    {
        if(empty($uid))
        {
            $this->commonTips('您还不是会员','','info');
            exit();
        }
    }

    /**
     * @author:橙橙同学
     * @func:格式化API返回信息
     */
    protected function commonApiReturn($errCode = 0,$data = '',$msg = '')
    {
        $return = array(
            'errCode'=>$errCode,
            'data'=>$data,
            'msg'=>$msg
        );
        echo json_encode($return);
        exit();
    }


    /**
     * @param $uid
     * @return bool
     * @author:橙橙同学
     * @func:获取会员信息
     */
    protected function getMemberInfoByUid($uid)
    {
        global $_W;
        $memberInfo = pdo_fetch("select * from ".tablename('mc_members')." where `uniacid`=:uniacid
        and `uid`=:uid",array(':uniacid'=>$_W['uniacid'],':uid'=>$uid));
        return $memberInfo;
    }


    public function doMobileTest()
    {
        include $this->template('companymember/applycompanymember');
    }

    public function doMobileEntry() {
		//这个操作被定义用来呈现 功能封面
        global $_W;
        $fan = \app\Oauth::checkFansAuth();

        if($fan!=''){
            $openid = $_W['openid'];
            $uid = mc_openid2uid($openid);
            $memberInfo = MemberModel::getMemberInfoByUid($uid);
            if(!empty($memberInfo))
            {
                if($memberInfo['groupid'] == 3){
                    //企业会员
                    include $this->template('member/companymember');
                }
                else{
                    //商业会员
                    include $this->template('member/businessmember');
                }
            }
            else{
                //非会员显示页面
            }
        }
        else{
            $this->commonTips('授权失败，请确保您在微信中打开本页面','','info');
            //经过微擎系统 微信授权 均没有获取到用户信息
        }
	}

    /**
     * @author: 橙橙同学
     * @fun:检测企业会员身份
     */
	protected function checkCompanyMember($memberInfo)
    {
        //企业会员grouoid = 3
        if(!empty($memberInfo) && $memberInfo['groupid'] == 3)
        {
            return true;
        }
        else{
            return false;
        }
    }


    protected function getBlockList()
    {
        global $_W;
        $attrValue = '街区';
        //街区id
        $blockAttrId = pdo_fetchcolumn("select * from ".tablename('storemanage_attr')."
            where `uniacid`=:uniacid and `value`=:attrvalue
        ",array('uniacid'=>$_W['uniacid'],'attrvalue'=>$attrValue));
        //带有街区标签的店铺id
        $storeIds =  pdo_fetchall("select store_id from ".tablename('storemanage_attrstore')."
            where `uniacid`=:uniacid and `attr_id`=:attr_id
        ",array('uniacid'=>$_W['uniacid'],'attr_id'=>$blockAttrId));

        if(!empty($storeIds)) {
            $storeIdsArr = array();
            foreach ($storeIds as $key => $value) {
                $storeIdsArr[] = $value['store_id'];
            }

            $inString = implode(',', $storeIdsArr);

            $stores = pdo_fetchall("select * from " . tablename('storemanage_store') . "
            where `uniacid`=:uniacid and `id` in (" . $inString . ")
        ", array('uniacid' => $_W['uniacid']));
            if (!empty($stores)) {

                foreach ($stores as $key=>$value)
                {
                    $storeArr[] = $value['storename'];
                }
                return $storeArr;
            }else{
                return '';
            }
        }else{
            return '';
        }
    }

    protected function getTranscriptList()
    {
        global $_W;
        $attrValue = '写字间';
        //街区id
        $AttrId = pdo_fetchcolumn("select * from ".tablename('storemanage_attr')."
            where `uniacid`=:uniacid and `value`=:attrvalue
        ",array('uniacid'=>$_W['uniacid'],'attrvalue'=>$attrValue));
        //带有街区标签的店铺id
        $storeIds =  pdo_fetchall("select store_id from ".tablename('storemanage_attrstore')."
            where `uniacid`=:uniacid and `attr_id`=:attr_id
        ",array('uniacid'=>$_W['uniacid'],'attr_id'=>$AttrId));

        if(!empty($storeIds)) {
            $storeIdsArr = array();
            foreach ($storeIds as $key => $value) {
                $storeIdsArr[] = $value['store_id'];
            }
            $inString = implode(',', $storeIdsArr);
            $stores = pdo_fetchall("select * from " . tablename('storemanage_store') . "
            where `uniacid`=:uniacid and `id` in (" . $inString . ")
        ", array('uniacid' => $_W['uniacid']));
            if (!empty($stores)) {
                foreach ($stores as $key=>$value)
                {
                    $storeArr[] = $value['storename'];
                }
                return $storeArr;
            }else{
                return '';
            }
        }else{
            return '';
        }
    }

    public function doMobileApiGetBlockList()
    {
        global $_W;
        $attrValue = '街区';
        //街区id
        $blockAttrId = pdo_fetchcolumn("select * from ".tablename('storemanage_attr')."
            where `uniacid`=:uniacid and `value`=:attrvalue
        ",array('uniacid'=>$_W['uniacid'],'attrvalue'=>$attrValue));
        //带有街区标签的店铺id
        $storeIds =  pdo_fetchall("select store_id from ".tablename('storemanage_attrstore')."
            where `uniacid`=:uniacid and `attr_id`=:attr_id
        ",array('uniacid'=>$_W['uniacid'],'attr_id'=>$blockAttrId));

        if(!empty($storeIds)) {
            $storeIdsArr = array();
            foreach ($storeIds as $key => $value) {
                $storeIdsArr[] = $value['store_id'];
            }

            $inString = implode(',', $storeIdsArr);

            $stores = pdo_fetchall("select * from " . tablename('storemanage_store') . "
            where `uniacid`=:uniacid and `id` in (" . $inString . ")
        ", array('uniacid' => $_W['uniacid']));
            if (!empty($stores)) {
                $this->commonApiReturn('0',$stores,'获取成功');
            }else{
                $this->commonApiReturn('-1','','空');
            }
        }else{
            $this->commonApiReturn('-1','','空');
        }
    }

    public function doMobileApiGetTranscriptList()
    {
        global $_W;
        $attrValue = '写字间';
        //街区id
        $AttrId = pdo_fetchcolumn("select * from ".tablename('storemanage_attr')."
            where `uniacid`=:uniacid and `value`=:attrvalue
        ",array('uniacid'=>$_W['uniacid'],'attrvalue'=>$attrValue));
        //带有街区标签的店铺id
        $storeIds =  pdo_fetchall("select store_id from ".tablename('storemanage_attrstore')."
            where `uniacid`=:uniacid and `attr_id`=:attr_id
        ",array('uniacid'=>$_W['uniacid'],'attr_id'=>$AttrId));

        if(!empty($storeIds)) {
            $storeIdsArr = array();
            foreach ($storeIds as $key => $value) {
                $storeIdsArr[] = $value['store_id'];
            }
            $inString = implode(',', $storeIdsArr);
            $stores = pdo_fetchall("select * from " . tablename('storemanage_store') . "
            where `uniacid`=:uniacid and `id` in (" . $inString . ")
        ", array('uniacid' => $_W['uniacid']));
            if (!empty($stores)) {
                $this->commonApiReturn('0',$stores,'获取成功');
            }else{
                $this->commonApiReturn('-1','','空');
            }
        }else{
            $this->commonApiReturn('-1','','空');
        }
    }


    /**
     * @author: 橙橙同学
     * @fun:申请企业会员页面
     */
	public function doMobileApplyCompanyMember()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
        $uid = mc_openid2uid($openid);
        //注意，mc_fetch有坑  缓存
        $memberInfo = $this->getMemberInfoByUid($uid);
        //1 申请待审核 2 已申请成功
        if($memberInfo['isapply']==1 && $memberInfo['groupid']!=3)
        {
            $this->commonTips('您已申请等待审核','http://vkwechat.mebaby.cn/app/index.php?i=2&c=mc','info');
        }


        $isCompanyMember = $this->checkCompanyMember($memberInfo);
        if($isCompanyMember)
        {
            $this->commonTips('您已经是企业会员，无需申请','http://vkwechat.mebaby.cn/app/index.php?i=2&c=mc','info');
        }else{
            if(checksubmit('submit')){
                //如果是提交事件
                if(empty($_GPC['member_name']) || empty($_GPC['member_mobile']) || empty($_GPC['company_attr']) || empty($_GPC['member_company']))
                {
                    $this->commonTips('信息不完整，请重新申请','','info');
                }else{
                    if($memberInfo['realname']!=$_GPC['member_name'] || $memberInfo['mobile']!=$_GPC['member_mobile'])
                    {
                        $this->commonTips('申请信息与会员信息不匹配，请核对','','info');
                    }else{
                        //公司类型安全性判断
                        if($_GPC['company_attr'] == '街区' || $_GPC['company_attr'] == '写字间')
                        {
                            if($_GPC['company_attr'] == '街区')
                            {
                                $stores = $this->getBlockList();
                            }else{
                                $stores = $this->getTranscriptList();
                            }

                            if(!in_array($_GPC['member_company'],$stores))
                            {
                                $this->commonTips('您输入的公司不在信息库中，请正确输入公司名称','','info');
                            }

                        }else{
                            $this->commonTips('您选择的公司类型不正确','','info');
                        }
                        // isapply变为1 填充公司信息
                        $updateData = array(
                            'isapply'=>1,
                            'company'=>$_GPC['member_company']
                        );
                        $updateMember = pdo_update('mc_members',$updateData,array('uid'=>$memberInfo['uid']));

                        if($updateMember)
                        {
                            $this->commonTips('申请成功，等待审核','http://vkwechat.mebaby.cn/app/index.php?i=2&c=mc','success');
                        }
                        else{
                            $this->commonTips('申请失败，请重新申请','http://vkwechat.mebaby.cn/app/index.php?i=2&c=mc','error');
                        }
                    }
                }
            }else{
                include $this->template('companymember/applycompanymember');
            }
        }
    }
}